﻿Option Explicit On

Module MODapplicationMain

    ' Declare Functions ---------------------------------------------------------------------------------------------------------------------------------

    Private Declare Sub GetSystemTime Lib "Kernel32" (ByVal oSystemTime As tSystemTime)

    ' Objects ---------------------------------------------------------------------------------------------------------------------------------

    Private fso As Scripting.FileSystemObject
    Private oLog As Scripting.TextStream

    ' Types -----------------------------------------------------------------------------------------------------------------------------------

    Private Structure tSystemTime

        Dim iYear As Integer
        Dim iMonth As Integer
        Dim iDayOfWeek As Integer
        Dim iDay As Integer
        Dim iHour As Integer
        Dim iMinute As Integer
        Dim iSecond As Integer
        Dim iMilliseconds As Integer

    End Structure

    Private oSystemTime As tSystemTime

    ' Constants -------------------------------------------------------------------------------------------------------------------------------

    Public Const iERROR As Integer = 1
    Public Const iWARNING As Integer = 2
    Public Const iINFO As Integer = 3
    Public Const iDEBUG As Integer = 4

    ' Strings ---------------------------------------------------------------------------------------------------------------------------------

    Dim _
        sModule As String, _
        sMessage As String, _
        sData As String, _
        sVBerrorObjectDescription As String

    ' Integers ----------------------------------------------------------------------------------------------------------------------------

    Dim iLevel As Integer

    ' Longs --------------------------------------------------------------------------------------------------------------------------------------

    Dim lVBerrorObjectNumber As Long

    ' begin code -----------------------------------------------------------------------------------------------------------------------------
    ' Logger function ----------------------------------------------------------------------------------------------------------------------------

    Public Sub logMessage( _
        ByVal iLevel As Integer, _
        ByVal sModule As String, _
        ByVal sMessage As String, _
        Optional ByVal sData As String = "", _
        Optional ByVal lVBerrorObjectNumber As Long = -1, _
        Optional ByVal sVBerrorObjectDescription As String = "")

        Dim sDateTime As String

        sDateTime = Date.Now.ToString & " " & Date.Now.TimeOfDay.ToString

        fso = New Scripting.FileSystemObject

        oLog = fso.OpenTextFile(sLogFilePath, Scripting.FileUse.ForAppending, True)

        oLog.WriteLine( _
            iLevel & vbTab & _
            Date.Now & " " & Date.Now.TimeOfDay.ToString & vbTab & _
            sModule & vbTab & _
            sMessage & vbTab & _
            Replace(sData, vbCrLf, "<LineBreak>") & vbTab & _
            lVBerrorObjectNumber & vbTab & _
            sVBerrorObjectDescription)

        oLog.Close()

        oLog = Nothing

        fso = Nothing

    End Sub

    ' Check version in registry and update registry if necessary -------------------------------------------------------------------

    Public Function checkVersion()

        With New regOp

            .Root = regOp.RegRoot.HKEY_LOCAL_MACHINE
            .Key = "Software\bbd\AccessSearch"

            If .Value("version") <> "4.2" Then
                .Value("version") = "4.2"
                .Key = "Software\bbd\AccessSearch\preferences"
                .Value("columnWidths") = ""
                .Value("defaultDatabasePath") = Application.ExecutablePath & "\localStock.mdb"
                .Value("defaultSearchField") = "partNumber"
                .Key = "Software\bbd\AccessSearch\preferences\remoteDB"
                .Value("autoUpdate") = 1
                .Value("fieldNames") = "partNumber,quantity,remark,manufacturer,dateCode"
                .Value("serverAddress") = "example : http://SERVER_ADDRESS"
                .Value("updatePagePath") = "example : FOLDER(s)/PAGE_NAME"
                .Value("databaseName") = "webStock.mdb"
            End If

        End With

    End Function

    ' Create Unique Time stamp -------------------------------------------------------------------------------------------------------------------

    Public Function getUniqueTimeStamp() As String

        ' call to API function to get current time
        Throw New NotImplementedException
        'GetSystemTime(oSystemTime)

        'getUniqueTimeStamp = _
        '    Year(Now) & "_" & _
        '    Month(Now) & "_" & _
        '    Day(Now) & "_" & _
        '    Hour(Now) & "_" & _
        '    Minute(Now) & "_" & _
        '    Second(Now) & "_" & _
        '    oSystemTime.iMilliseconds

    End Function

    ' Properties --------------------------------------------------------------------------------------------------------------------------------

    ReadOnly Property sLogFilePath() As String
        Get

            sLogFilePath = Application.ExecutablePath & "/AccessSearch.log"
        End Get


    End Property

End Module
